package com.iseastar.guojiang.wallet;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.WalletTakeMoneyHistoryBean;
import com.shouhuobao.station.R;
import com.shouhuobao.take.weight.CircularImage;

import droid.frame.activity.title.TitleRes;
import droid.frame.utils.GlideUtil;
import droid.frame.view.xlist.FooterListener;
import droid.frame.view.xlist.SListView;

/**
 * Created by joker on 2018/1/25.
 * 火箭侠提现记录
 */
public class WalletTakeMoneyHistoryActivity extends BaseActivity2 {

    private SListView mListview;
    private WalletTakeMoneyHistoryAdapter mAdapter;
    private ReqResult<WalletTakeMoneyHistoryBean> result = null;
    private String openId;
    private String name;
    private String headImgUrl;
    private LinearLayout mUnBoundLL;
    private LinearLayout mBindLL;
    private CircularImage mTitleIV;
    private TextView mNickNameTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        openId = getIntent().getStringExtra("openId");
        name = getIntent().getStringExtra("name");
        headImgUrl = getIntent().getStringExtra("headImgUrl");
        super.onCreate(savedInstanceState);
        updateWXInfoView();

        showLoadingDialog(null);
        AppHttp.getInstance().courierWalletTakeMoneyRecord(1);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.wallet_take_money_history);
        super.findViewById();
        getAppTitle().setCommonTitle("提现记录", new TitleRes("联系客服", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogMgr.showPhoneDialog(getContext(), null);
            }
        }));
        mListview = (SListView) findViewById(R.id.listView);
        mAdapter = new WalletTakeMoneyHistoryAdapter(null, getContext());
        mListview.setAdapter(mAdapter);
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppHttp.getInstance().courierWalletTakeMoneyRecord(1);
            }
        });
        mListview.setFooterListener(new FooterListener() {
            @Override
            public void onFooterVisible() {
                //如果有条件的话，就按条件来请求，负责按照全部请求
                if (result != null && result.getPageNum() < result.getTotalPage()) {
                    AppHttp.getInstance().courierWalletTakeMoneyRecord(result.getPageNum() + 1);
                }
            }
        });

        mUnBoundLL = (LinearLayout) findViewById(R.id.unbound_ll);
        mBindLL = (LinearLayout) findViewById(R.id.bind_ll);
        findViewById(R.id.bind_now_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), CourierBindWXActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("openId", openId);
                intent.putExtra("headImageUrl", headImgUrl);
                startActivityForResult(intent, 101);
            }
        });
        mTitleIV = (CircularImage) findViewById(R.id.title_img);
        mNickNameTV = (TextView) findViewById(R.id.nick_name_tv);

    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //提现记录数据返回
            case MsgID.courier_wallet_take_money_record:
                cancelLoadingDialog();
                onRefreshComplete();
                result = JSON.parseList(msg.obj, WalletTakeMoneyHistoryBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (checkLoginStatus(result)) {
                            if (mAdapter == null) {
                                mAdapter = new WalletTakeMoneyHistoryAdapter(null, getContext());
                                mListview.setAdapter(mAdapter);
                            }
                            if (result.getPageNum() == 1) {
                                mAdapter.setItems(result.getResultList(), true);
                            } else {
                                mAdapter.appendItems(result.getResultList(), true);
                            }
                            mListview.updateFooter(result.getPageNum(), result.getTotalPage());

                        } else {
                            showToast(result.getMessage());
                        }
                    }
                });
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == 101) {
            name = data.getStringExtra("name");
            headImgUrl = data.getStringExtra("headImageUrl");
            openId = data.getStringExtra("openId");
            updateWXInfoView();
        }
    }

    /**
     * 更新微信是否绑定状态
     */
    private void updateWXInfoView() {
        if (isNotEmpty(openId)) {
            mUnBoundLL.setVisibility(View.GONE);
            mBindLL.setVisibility(View.VISIBLE);
            GlideUtil.loadImage(getContext(), headImgUrl, 0, mTitleIV);
            mNickNameTV.setText(name);
        } else {
            mUnBoundLL.setVisibility(View.VISIBLE);
            mBindLL.setVisibility(View.GONE);
        }
    }

    @Override
    public void finish() {
        Intent intent = new Intent();
        intent.putExtra("name", name);
        intent.putExtra("headImageUrl", headImgUrl);
        intent.putExtra("openId", openId);
        setResult(Activity.RESULT_OK, intent);
        super.finish();
    }
}
